package com.yanqu.road.server.cmd.banquet.party;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.invitation.BanquetMarryInvitation;
import com.yanqu.road.entity.enums.eBanquetInfoType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.CrossMarriageGroupMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

//@Cmd(code = GameProtocol.S_PARTY_OPEN, desc = "")
@Cmd(code = Protocol.S_BANQUET_SERVER_OPEN, desc = "开宴会")
public class OpenPartyCmd extends AbstractCommand {
    private int type;
    private String invitationId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.OpenBanquetReq reqMsg = BanquetProto.OpenBanquetReq.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        invitationId = reqMsg.getInvitationId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (BanquetConfigMgr.isServerNormalBanquet(type) && !SystemOpenMgr.systemOpen(player, eSystemId.Banquet.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if (BanquetConfigMgr.isServerFeastBanquet(type) && !SystemOpenMgr.systemOpen(player, eSystemId.BanquetServer.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        BanquetModule banquetModule = player.getModule(BanquetModule.class);
        if (BanquetConfigMgr.isBanquetInvitation(type)) {
            if (!banquetModule.checkUserInvitation(invitationId, type)) {
                ret = GameErrorCode.E_BANQUET_INVITATION;
                return;
            }
        }
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(type);
        if (banquetInfo == null) {
            ret = GameErrorCode.E_BANQUET_INFO_ERROR;
            return;
        }
        String consume = "";
        if (!"0".equals(banquetInfo.getOpenConsume())) {
            consume = banquetInfo.getOpenConsume();
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(banquetInfo.getOpenConsume()))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
        }

        if (banquetInfo.getId() == 10003 || banquetInfo.getId() == 10004) {
            int vipLv = VipMgr.getVipLv(player.getUserInfo().getVipExp());
            if (vipLv < GameConfig.BANQUET_PRIVATE_VIP_LIMIT) {
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
        }

        if (!StringUtils.isNullOrEmpty(consume)) {
            player.getModule(PreReduceModule.class).preReduce(consume);
        }

        //假如是请帖开启要保存请帖类型
        String invitationInfo = "";
        if (BanquetConfigMgr.isBanquetInvitation(type)) {
            invitationInfo = banquetModule.useUserInvitation(invitationId, type);
            try {
                if (type == eBanquetInfoType.MarryBanquet.getValue()) {
                    BanquetMarryInvitation invitation = JSON.parseObject(invitationInfo, BanquetMarryInvitation.class);
                    int beautyId = invitation.getBeautyId();
                    UserBeauty userBeauty = player.getModule(BeautyModule.class).getUserBeauty(beautyId);
                    if (userBeauty != null) {
                        invitation.setIntimacy(userBeauty.getTotalIntimacy());
                    }
                    invitationInfo = JSON.toJSONString(invitation);
                }

            } catch (Exception e) {
                log.error("{} openParty error.", player.getUserId());
            }
        }
        BanquetProto.CrossOpenPartyReqMsg.Builder msg = BanquetProto.CrossOpenPartyReqMsg.newBuilder();
        msg.setGroup(0);
        msg.setType(type);
        msg.setInvitationInfo(invitationInfo);
        msg.setInvitationId(invitationId);
        msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        List<Long> partyServerList = CrossMarriageGroupMgr.getPartyServerList(GameServer.getInstance().getServerId());
        msg.setServerListStr(StringUtils.listToString(partyServerList, ","));
        msg.setGiftTypeAccept(banquetModule.getUserBanquetSettingGiftTypeAccept(type));
        player.sendPacket(CrossProtocol.C_CROSS_PARTY_OPEN, msg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
